﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyBLL;
using MyEntity;

//using my

namespace MyUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //实例化BLL类
        public ProfessionInfoBLL professionInfoBLL = new ProfessionInfoBLL();
        public StudentInfoBLL studentInfoBLL = new StudentInfoBLL();

        //绑定combox中的数据
        private void BindPro()
        {
            List<ProfessionInfoEntity> list = new List<ProfessionInfoEntity>();
            list = professionInfoBLL.List();
            list.Insert(0, new ProfessionInfoEntity { ProfessionID = 0, ProfessionName = "请选择" });
            this.cmdName.DataSource = list;
            //显示内容
            this.cmdName.DisplayMember = "ProfessionName";
            //编号
            this.cmdName.ValueMember = "ProfessionID";
        }

        //处理datagridview的数据
        private void BindData()
        {
            StudentInfoEntity stu = new StudentInfoEntity();
            stu.ProfessionID = Convert.ToInt32(this.cmdName.SelectedValue.ToString());
            stu.StuName = this.stuname.Text.Trim();
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = studentInfoBLL.Search(stu);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindPro();
            BindData();
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString() == "Yes")
            {
                string ID = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                int count = studentInfoBLL.Delete(ID);
                if (count > 0)
                {
                    MessageBox.Show("删除成功");
                }
                BindData();
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            FrmAdd frmAdd = new FrmAdd();
            frmAdd.Owner = this;
            frmAdd.FormClosed += (s, c) =>
            {
                this.BindData();
            };
            frmAdd.Show();
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            string ID = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            MessageBox.Show(ID);
            FrmEdit frmEdit = new FrmEdit(ID);

            frmEdit.FormClosed += (s, c) =>
            {
                this.BindData();
            };
            frmEdit.Show();
        }
    }
}
